package me.zhengjie.minbearwcs.utils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.minbearwcs.entity.DTO.inbound.mes.order.MesInboundOrder;
import me.zhengjie.minbearwcs.entity.DTO.inbound.wms.WmsPalletArriveExchangeArea;
import me.zhengjie.minbearwcs.entity.DTO.response.Resp;
import me.zhengjie.minbearwcs.entity.DTO.rework.wms.WmsReworkArrive;
import me.zhengjie.minbearwcs.entity.DTO.storage.wms.WmsStorageUpdate;
import me.zhengjie.minbearwcs.entity.DTO.wms.inbound.MinBearInboundRequestDTO;
import me.zhengjie.minbearwcs.entity.DTO.wms.inbound.MinBearInboundDataResponseDTO;
import me.zhengjie.minbearwcs.entity.DTO.wms.inbound.MinBearInboundResultDTO;
import me.zhengjie.minbearwcs.entity.DTO.wms.login.MinBearLoginDTO;
import me.zhengjie.minbearwcs.entity.DTO.wms.login.MinBearLoginResultDTO;
import me.zhengjie.minbearwcs.entity.DTO.wms.materialType.MinBearMaterialTypeDTO;
import me.zhengjie.minbearwcs.entity.DTO.wms.materialType.MinBearMaterialTypeResultDTO;
import me.zhengjie.minbearwcs.entity.DTO.wms.storageUpdate.MinBearStorageUpdateRequestDTO;
import me.zhengjie.minbearwcs.entity.DTO.wms.storageUpdate.MinBearStorageUpdateDataResponseDTO;
import me.zhengjie.minbearwcs.entity.DTO.wms.storageUpdate.MinBearStorageUpdateResultDTO;
import me.zhengjie.minbearwcs.entity.PO.config.HttpConfig;
import me.zhengjie.minbearwcs.entity.PO.wcs.stock.MaterialType;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import me.zhengjie.minbearwcs.dao.config.inf.HttpConfigDao;
import me.zhengjie.minbearwcs.entity.DTO.agv.TUSK.TUSKMission;
import me.zhengjie.minbearwcs.entity.DTO.agv.TUSK.TUSKResponse;

import java.util.HashMap;
import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class WcsHpptUtil {

    private final RestTemplate restTemplate;

    private final HttpConfigDao httpConfigDao;


    public TUSKResponse dispatchTUSKMission(TUSKMission mission) {
        log.info("发送TUSK任务: {}", JsonUtil.getJsonInfo(mission));
        HttpConfig tuskHttpConfig = httpConfigDao.selectByHttpName(HttpConfigContentPool.TUSK_IP_PORT);
        String url = tuskHttpConfig.getConfigIp() + "/rpc/createTask";
        TUSKResponse tuskResponse = restTemplate
                .exchange(url,
                        HttpMethod.POST,
                        new HttpEntity<>(mission),
                        new ParameterizedTypeReference<TUSKResponse>() {
                        })
                .getBody();

        log.info("收到TUSK任务下发回复 ： {}", JsonUtil.getJsonInfo(tuskResponse));
        return tuskResponse;
    }


    public TUSKResponse cancelTUSKMission(String taskId) {
        HashMap<String, String> cancelTaskMap = new HashMap<>();
        cancelTaskMap.put("taskId",taskId);
        log.info("发送TUSK任务取消: {}", JsonUtil.getJsonInfo(cancelTaskMap));
        HttpConfig tuskHttpConfig = httpConfigDao.selectByHttpName(HttpConfigContentPool.TUSK_IP_PORT);
        String url = tuskHttpConfig.getConfigIp() + "/rpc/cancelTask";
        TUSKResponse tuskResponse = restTemplate
                .exchange(url,
                        HttpMethod.POST,
                        new HttpEntity<>(cancelTaskMap),
                        new ParameterizedTypeReference<TUSKResponse>() {
                        })
                .getBody();

        log.info("收到TUSK任务取消回复 ： {}", JsonUtil.getJsonInfo(tuskResponse));
        return tuskResponse;
    }


    public Resp notifyWmsPalletArriveExchangeArea(WmsPalletArriveExchangeArea source) {
        HttpConfig httpConfig = httpConfigDao.selectByHttpName(HttpConfigContentPool.XIAO_XIONG_WMS_IP_PORT);
        String url = httpConfig.getConfigIp() + "/inbound/check";
        return restTemplate
                .exchange(url,
                        HttpMethod.POST,
                        new HttpEntity<>(source),
                        new ParameterizedTypeReference<Resp>() {
                        })
                .getBody();

    }




    public Resp wmsShiftingStorageUpdate(WmsStorageUpdate source) {
        String ticket = loginAndTicket();

        HttpConfig httpConfig = httpConfigDao.selectByHttpName(HttpConfigContentPool.XIAO_XIONG_WMS_IP_PORT);
        String url = httpConfig.getConfigIp() + "/shifting/storage/update";


        MinBearStorageUpdateRequestDTO minBearStorageUpdateRequestDTO = new MinBearStorageUpdateRequestDTO(source, ticket);
        HttpEntity<Object> objectHttpEntity = new HttpEntity<>(minBearStorageUpdateRequestDTO, addHeadAuth());

        log.info("发送小熊移库储位更新获取：{}", JsonUtil.getJsonInfo(minBearStorageUpdateRequestDTO));

        ResponseEntity<MinBearStorageUpdateResultDTO> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                objectHttpEntity,
                new ParameterizedTypeReference<MinBearStorageUpdateResultDTO>() {
                });


        log.info("获取小熊移库储位更新信息：{}", JsonUtil.getJsonInfo(response.getBody()));
        return Resp.build()
                .code(response.getBody().getData().getSuccess())
                .reason(response.getBody().getMessage());

    }

    public Resp wmsStorageUpdate(WmsStorageUpdate source) {
        String ticket = loginAndTicket();

        HttpConfig httpConfig = httpConfigDao.selectByHttpName(HttpConfigContentPool.XIAO_XIONG_WMS_IP_PORT);
        String url = httpConfig.getConfigIp() + "/storage/update";


        MinBearStorageUpdateRequestDTO minBearStorageUpdateRequestDTO = new MinBearStorageUpdateRequestDTO(source, ticket);
        HttpEntity<Object> objectHttpEntity = new HttpEntity<>(minBearStorageUpdateRequestDTO, addHeadAuth());

        log.info("发送小熊储位更新获取：{}", JsonUtil.getJsonInfo(minBearStorageUpdateRequestDTO));

        ResponseEntity<MinBearStorageUpdateResultDTO> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                objectHttpEntity,
                new ParameterizedTypeReference<MinBearStorageUpdateResultDTO>() {
                });


        log.info("获取小熊储位更新信息：{}", JsonUtil.getJsonInfo(response.getBody()));
        return Resp.build()
                .code(response.getBody().getData().getSuccess())
                .reason(response.getBody().getMessage());

    }


    public MesInboundOrder fetchInboundOrder(String palletCode) {
        String ticket = loginAndTicket();

        HttpConfig httpConfig = httpConfigDao.selectByHttpName(HttpConfigContentPool.XIAO_XIONG_WMS_IP_PORT);
        String url = httpConfig.getConfigIp() + "/inbound/fetch";

        MinBearInboundRequestDTO minBearInboundRequestDTO = new MinBearInboundRequestDTO(palletCode, ticket);
        HttpEntity<Object> objectHttpEntity = new HttpEntity<>(minBearInboundRequestDTO, addHeadAuth());

        log.info("发送小熊入库单获取：{}", JsonUtil.getJsonInfo(minBearInboundRequestDTO));

        ResponseEntity<MinBearInboundResultDTO> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                objectHttpEntity,
                new ParameterizedTypeReference<MinBearInboundResultDTO>() {
                });


        log.info("获取小熊入库单信息：{}", JsonUtil.getJsonInfo(response.getBody()));
        return response.getBody().getData().getResult();

    }

    public List<MaterialType> fetchMaterialTypeList(String formatDate) {
        String ticket = loginAndTicket();

        HttpConfig httpConfig = httpConfigDao.selectByHttpName(HttpConfigContentPool.XIAO_XIONG_WMS_IP_PORT);
        String url = httpConfig.getConfigIp() + "/material/sync";

//        String format = LocalDateTime.now().format(ContentPool.DATE_FORMAT_YEAR_MONTH_DAY);
        MinBearMaterialTypeDTO minBearMaterialTypeDTO = new MinBearMaterialTypeDTO(formatDate, ticket);
        HttpEntity<Object> objectHttpEntity = new HttpEntity<>(minBearMaterialTypeDTO, addHeadAuth());

        log.info("发送小熊物料信息获取：{}", JsonUtil.getJsonInfo(minBearMaterialTypeDTO));

        ResponseEntity<MinBearMaterialTypeResultDTO> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                objectHttpEntity,
                new ParameterizedTypeReference<MinBearMaterialTypeResultDTO>() {
                });


        log.info("获取小熊物料信息：{}", JsonUtil.getJsonInfo(response.getBody()));
        return response.getBody().getMaterialTypeList();

    }


    // 小熊加auth
    private HttpHeaders addHeadAuth() {
        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBasicAuth("wcs-XI8W96MR", "CnF3w2fRpaWM9c0o7AgMPdk0U2eD5Qxy");
        return headers;
    }

    // 小熊登录
    private String loginAndTicket() {
        HttpConfig httpConfig = httpConfigDao.selectByHttpName(HttpConfigContentPool.XIAO_XIONG_WMS_IP_PORT);
        String url = httpConfig.getConfigIp() + "/login";
        HttpEntity<MinBearLoginDTO> objectHttpEntity = new HttpEntity<>(new MinBearLoginDTO(), addHeadAuth());


        ResponseEntity<MinBearLoginResultDTO> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                objectHttpEntity,
                new ParameterizedTypeReference<MinBearLoginResultDTO>() {
                });

        log.info("获取小熊登录信息：{}", JsonUtil.getJsonInfo(response.getBody()));
        return response.getBody().getContext().getTicket();
    }
}
